﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using WorkItemRSS.Properties;

namespace WorkItemRSS
{
    public class Subscriber
    {
        public Subscriber()
        {

        }
        public string GetDisplayNameAndUpdateChart(string LogonUserIdentityName)
        {
            string AssignedTo = string.Empty;
            DAL oDAL = new DAL();
             try
             {
                 AssignedTo = oDAL.GetSubscriberByLogonUserIdentityName(LogonUserIdentityName);
             }
             catch{}
             if (string.IsNullOrEmpty(AssignedTo))
             {
                 try
                 {
                     
                     DirectoryEntry UserDirectoryEntry = new DirectoryEntry(Settings.Default.ADURL);
                     UserDirectoryEntry.Username = Settings.Default.ADUserName;
                     UserDirectoryEntry.Password = Settings.Default.ADUserPassword;

                     DirectorySearcher Searcher = new DirectorySearcher(UserDirectoryEntry);
                     Searcher.CacheResults = true;
                     Searcher.SearchScope = SearchScope.Subtree;

                     string strUsername = LogonUserIdentityName.Substring(LogonUserIdentityName.IndexOf("\\") + 1);
                     string DomainName = LogonUserIdentityName.Substring(0, LogonUserIdentityName.IndexOf("\\"));

                     Searcher.Filter = string.Format("(&(objectCategory=Person)(sAMAccountName={0}))", strUsername);

                     Searcher.PropertiesToLoad.Add("displayName");
                     Searcher.PropertiesToLoad.Add("manager");
                     Searcher.PropertiesToLoad.Add("directReports");

                     Searcher.PageSize = 1000;
                     SearchResult result = Searcher.FindOne();
                     string ManagerName = string.Empty;
                     if (result != null)
                     {
                         if (result.Properties["displayName"] != null)
                             AssignedTo = result.Properties["displayName"][0].ToString();
                         try
                         {
                             if (result.Properties["manager"].Count > 0)
                             {
                                 DirectoryEntry oManagerDirectoryEntry = new DirectoryEntry("LDAP://" + result.Properties["manager"][0].ToString().Trim(), Settings.Default.ADUserName, Settings.Default.ADUserPassword);
                                 ManagerName = oManagerDirectoryEntry.Properties["displayName"].Value.ToString();
                                 oManagerDirectoryEntry.Dispose();
                             }
                             if (result.Properties["directReports"].Count > 0)
                             {
                                 for (int j = 0; j < result.Properties["directReports"].Count; j++)
                                 {
                                     DirectoryEntry oEmployeeDirectoryEntry = new DirectoryEntry("LDAP://" + result.Properties["directReports"][j].ToString().Trim(), Settings.Default.ADUserName, Settings.Default.ADUserPassword);
                                     string EmployeeLogonUserIdentityName = DomainName + "\\" + oEmployeeDirectoryEntry.Properties["sAMAccountName"].Value.ToString();
                                     string EmployeeDisplayName = oEmployeeDirectoryEntry.Properties["displayName"].Value.ToString();
                                     string EmployeeManager = AssignedTo;
                                     oDAL.UpdateSubscriber(EmployeeLogonUserIdentityName, EmployeeDisplayName, EmployeeManager, false);
                                     oEmployeeDirectoryEntry.Dispose();
                                 }
                             }
                             else
                                 oDAL.UpdateSubscriber(LogonUserIdentityName, AssignedTo, ManagerName, true);
                         }
                         catch { }
                     }
                     Searcher.Dispose();
                     UserDirectoryEntry.Dispose();
                     if (!string.IsNullOrEmpty(AssignedTo))
                     {

                         oDAL.UpdateSubscriber(LogonUserIdentityName, AssignedTo, ManagerName, true);
                     }
                     
                 }
                 catch { }
             }
             
            return AssignedTo;
        }

       
    }
}